package com.freeheart.validation.constraintvalidators.in;


import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;

import java.beans.Introspector;

/**
 * 从Spring容器中获取InCollection实例，注意：使用Class和beanName获取Spring Bean，
 * beanName使用的是:类名首字母小写的方式。
 *
 * @author: EmperorHeart
 * @date: 2025/5/9 23:17
 * @description
 */
public class SpringInCollectionSupplier implements InCollectionSupplier, Ordered {

    @Override
    public boolean support() {
        // 如果存在，说明存在Spring IOC容器
        return SpringUtil.getApplicationContext() != null;
    }

    @Override
    public InCollection getInCollectionInstance(Class<? extends InCollection> clazz) {
        String beanName = Introspector.decapitalize(clazz.getSimpleName());
        ApplicationContext context = SpringUtil.getApplicationContext();
        return context.getBean(clazz, beanName);
    }


    @Override
    public int getOrder() {
        return -1;
    }
}
